#!/bin/bash -e

# log helper base on environment variable CONTAINER_LOG_LEVEL
# CONTAINER_LOG_LEVEL environment variable is set by run tool based on --log-level argument (info by default)
# or you can set it directly with docker --env argument

# Usage example: log-helper info CONTAINER_LOG_LEVEL is info or more
# the message "CONTAINER_LOG_LEVEL is info or more" will be printed only if log level is info, debug or trace

LOG_LEVEL_NONE=0
LOG_LEVEL_ERROR=1
LOG_LEVEL_WARNING=2
LOG_LEVEL_INFO=3
LOG_LEVEL_DEBUG=4
LOG_LEVEL_TRACE=5

# default log level if CONTAINER_LOG_LEVEL is not set -> info
log_level=${CONTAINER_LOG_LEVEL:-${LOG_LEVEL_INFO}}

call=$1 # function to call (error, warning, info, debug, trace, level)
if [[ ! "$call" =~ ^(error|warning|info|debug|trace|level)$ ]]; then
  echo "Error: Function $call not found"
  echo "Allowed functions are: error, warning, info, debug, trace, level"
  echo "usage example: log-helper info hello !"
  exit 1
fi


echo_msg="" # message to print if required log level is set
echo_param="" # echo command parameters

function error() {
  getEchoParams $@

  if [ $log_level -ge 1 ]; then
    echo $echo_param  "$echo_msg"
  fi
}

function warning() {
  getEchoParams $@

  if [ $log_level -ge 2 ]; then
    echo $echo_param  "$echo_msg"
  fi
}

function info() {
  getEchoParams $@

  if [ $log_level -ge 3 ]; then
    echo $echo_param "$echo_msg"
  fi
}

function debug() {
  getEchoParams $@

  if [ $log_level -ge 4 ]; then
    echo $echo_param "$echo_msg"
  fi
}

function trace() {
  getEchoParams $@

  if [ $log_level -ge 5 ]; then
    echo $echo_param "$echo_msg"
  fi
}

function getMsgFromStdin() {
  if [ -z "$2" ]; then
    echo_msg=$(cat)
  fi
}

function getEchoParams() {

  echo_msg="$@"

  if [[ "$1" =~ ^(-e|-n|-E)$ ]]; then
    echo_param=$1
    echo_msg=${echo_msg#$1 }
  fi

  # read from pipe if echo_msg is empty
  [[ -n "$echo_msg" ]] || getMsgFromStdin
}

function level() {

  local operator=$1
  local loglevel_str=$2
  local loglevel_str=${loglevel_str^^} # uppercase

  if [[ ! "$operator" =~ ^(eq|ne|gt|ge|lt|le)$ ]]; then
    echo "Error: Operator $operator not allowed"
    echo "Allowed operators are: eq, ne, gt, ge, lt, le"
    echo "Help: http://www.tldp.org/LDP/abs/html/comparison-ops.html"
    exit 1
  fi

  if [ -z "$loglevel_str" ]; then
    echo "Error: No log level provided"
    echo "Allowed log level are: none, error, warning, info, debug, trace"
    echo "usage example: log-helper level eq info"
    exit 1
  fi

  local log_level_var=LOG_LEVEL_$loglevel_str

  if [ $log_level -$operator ${!log_level_var} ]; then
    exit 0
  else
    exit 1
  fi
}

shift
$call "$@"

exit 0
